using System;
using System.IO;
using System.Web.UI.WebControls;

using Impact.Model.Objects;
using Impact.Controllers;
using Impact.Web.Foundation.Controls;

namespace Impact.Web.Admin
{
    public partial class EditPhoto : Impact.Web.Foundation.PageBase
    {
        /// <summary>
        /// Loading event handler
        /// </summary>
        protected override void OnLoad( EventArgs e )
        {

            if ( string.IsNullOrEmpty( Request.QueryString["id"] ) )
            {
                Response.Redirect( "Photos.aspx" );
            }

            base.OnLoad( e );
        }

        /// <summary>
        /// data binding specific to this page
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            Photo photo = PhotoController.GetPhoto( Request.QueryString["id"] );
            thumbnail.ImageUrl = ResolveClientUrl( PhotoController.GetThumbnailPath( photo ) );
            caption.Text = photo.Caption;

        }

        /// <summary>
        /// event handler executed when the user clicks Submit
        /// </summary>
        protected void SubmitCommand( object sender, EventArgs e )
        {
            Photo photo = PhotoController.GetPhoto( Request.QueryString["id"] );
            photo.Caption = caption.Text;
            PhotoController.SavePhoto( photo );

            MessageBox.Show( "Photo Saved" );
            Return();
        }

        /// <summary>
        /// event handler executed when the user clicks Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CancelCommand( object sender, EventArgs e )
        {
            Return();
        }

        private new void Return()
        {
            Photo photo = PhotoController.GetPhoto( Request.QueryString["id"] );
            Response.Redirect( string.Format( "Photos.aspx?id={0}", photo.GalleryID ) );
        }

    }
}
